﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPTHelper.BLL
{
    /// <summary>
    /// 单位换算
    /// </summary>
    public static class UnitConverter
    {
        public const double DefaultDpi = 96;

        #region Pixel

        public static Pixel ToPixel(this Inch inch) => inch.ToEmu().ToPixel();

        public static Inch ToInch(this Pixel pixel) => pixel.ToEmu().ToInch();

        #endregion

        #region Dxa

        /// <summary>
        /// Dxa转换为像素点
        /// </summary>
        /// <param name="dxa"></param>
        /// <returns></returns>
        public static Pt ToPt(this Dxa dxa)
        {
            return new Pt(dxa.Value / 20);
        }

        /// <summary>
        /// 像素点转换为Dxa
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public static Dxa ToDxa(this Pt pt)
        {
            return new Dxa(pt.Value * 20);
        }

        /// <summary>
        /// Dxa转换为像素点：好像这个方法是错误的。
        /// </summary>
        /// <param name="dxa"></param>
        /// <returns></returns>
        public static Inch ToInch(this Dxa dxa)
        {
            return new Inch(dxa.Value / 72);
        }

        public static Dxa ToDxa(this Inch inch)
        {
            return new Dxa(inch.Value * 72);
        }

        #endregion

        #region Mm

        /// <summary>
        /// 毫米转换为厘米
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        public static Cm ToCm(this Mm mm)
        {
            return new Cm(mm.Value / 10);
        }

        /// <summary>
        /// 厘米转换为毫米
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        public static Mm ToMm(this Cm cm)
        {
            return new Mm(cm.Value * 10);
        }

        #endregion

        #region Pt

        public static Cm ToCm(this Pt pt)
        {
            return pt.ToEmu().ToCm();
        }

        public static Pt ToPt(this Cm cm)
        {
            return cm.ToEmu().ToPt();
        }

        public static Mm ToMm(this Pt pt)
        {
            return pt.ToCm().ToMm();
        }

        public static Pt ToPt(this Mm mm)
        {
            return mm.ToCm().ToPt();
        }

        public static Pt ToPt(this HalfPoint halfPoint)
        {
            return new Pt(halfPoint.Value / 2);
        }

        public static HalfPoint ToHalfPoint(this Pt pt)
        {
            return new HalfPoint(pt.Value * 2);
        }


        public static Pixel ToPixel(this Pt pt)
        {
            return new Pixel(pt.Value / 72 * DefaultDpi);
        }

        public static Pt ToPoint(this Pixel px)
        {
            return new Pt(px.Value * 72 / DefaultDpi);
        }

        #endregion

        #region Emu

        public static Emu ToEmu(this Inch inch)
        {
            return new Emu(inch.Value * 914400);
        }

        public static Inch ToInch(this Emu emu)
        {
            return new Inch(emu.Value / 914400);
        }

        public static Emu ToEmu(this Cm cm)
        {
            return new Emu(cm.Value * 360000);
        }

        public static Cm ToCm(this Emu emu)
        {
            return new Cm(emu.Value / 360000);
        }

        public static Emu ToEmu(this Mm cm)
        {
            return new Emu(cm.Value * 36000);
        }

        public static Dxa ToDxa(this Emu emu)
        {
            return new Dxa(emu.Value / 635);
        }

        public static Emu ToEmu(this Dxa dxa)
        {
            return new Emu(dxa.Value * 635);
        }

        public static Mm ToMm(this Emu emu)
        {
            return new Mm(emu.Value / 36000);
        }


        public static Emu ToEmu(this Pixel px)
        {
            return new Emu(px.Value * 914400 / DefaultDpi);
        }

        public static Pixel ToPixel(this Emu emu)
        {
            return new Pixel(emu.Value / 914400 * DefaultDpi);
        }

        public static Emu ToEmu(this Pt pt)
        {
            return new Emu(pt.Value * 12700);
        }

        /// <summary>
        /// 914400 / 72 = 12700
        /// </summary>
        /// <param name="emu"></param>
        /// <returns></returns>
        public static Pt ToPt(this Emu emu)
        {
            return new Pt(emu / 12700);
        }

     

        #endregion
    }
}
